/**
 * 表单对象赋值:
 * 对目标对象存在且源对象同样存在的属性，全部覆盖；
 * 目标对象不存在但是源对象存在的属性， 全部丢弃；
 * 目标对象存在但是源对象不存在的属性，如果是字符串赋值为空串，其余类型赋值为undefined
 */
export function recover(target, source) {
	if (target === undefined || target === null) {
		throw new TypeError('Cannot convert first argument to object')
	}
	var to = Object(target)
	if (source === undefined || source === null) {
		return to
	}
	var keysArray = Object.keys(Object(target))
	for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
		var nextKey = keysArray[nextIndex]
		var desc = Object.getOwnPropertyDescriptor(target, nextKey)
		if (desc !== undefined && desc.enumerable) {
			if (to.hasOwnProperty(nextKey)) {
				if (to[nextKey] instanceof Array) {
					to[nextKey] = source[nextKey]
				} else if (to[nextKey] instanceof Object) {
					recover(to[nextKey], source[nextKey])
				} else if (source[nextKey] !== undefined) {
					to[nextKey] = source[nextKey]
				} else if (typeof to[nextKey] === 'string') {
					to[nextKey] = ''
				} else {
					to[nextKey] = undefined
				}
			}
		}
	}
	return to
}

/**
 * 表单对象赋值:
 * 对目标对象存在且源对象同样存在的属性，全部覆盖；
 * 目标对象不存在但是源对象存在的属性， 全部丢弃；
 * 目标对象存在但是源对象不存在的属性，保留目标对象的属性不做处理
 */
export function recoverNotNull(target, source) {
	if (target === undefined || target === null) {
		throw new TypeError('Cannot convert first argument to object')
	}
	var to = Object(target)
	if (source === undefined || source === null) {
		return to
	}
	var keysArray = Object.keys(Object(target))
	for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
		var nextKey = keysArray[nextIndex]
		var desc = Object.getOwnPropertyDescriptor(target, nextKey)
		if (desc !== undefined && desc.enumerable) {
			if (to.hasOwnProperty(nextKey)) {
				if (to[nextKey] instanceof Array) {
					to[nextKey] = source[nextKey]
				} else if (to[nextKey] instanceof Object) {
					recover(to[nextKey], source[nextKey])
				} else if (source[nextKey] !== undefined) {
					to[nextKey] = source[nextKey]
				}
			}
		}
	}
	return to
}

/**
 * 树形数据转换
 * @param {*} data list数据
 * @param {*} id 主键ID
 * @param {*} pid 上级ID
 * @param childrenKey 子list数据的key
 */
export function treeDataTranslate(data, id = 'id', pid = 'parentId', childrenKey = 'childNodes') {
  let res = []
  let temp = {}
  for (let i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (let k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]][childrenKey]) {
        temp[data[k][pid]][childrenKey] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]][childrenKey].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

export function escapeHTML(a) {
	a = '' + a
	return a
		.replace(/&/g, '&amp;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
		.replace(/"/g, '&quot;')
		.replace(/'/g, '&apos;')
}
/**
 * @function unescapeHTML 还原html脚本 < > & " '
 * @param a - 字符串
 */
export function unescapeHTML(a) {
	a = '' + a
	return a
		.replace(/&lt;/g, '<')
		.replace(/&gt;/g, '>')
		.replace(/&amp;/g, '&')
		.replace(/&quot;/g, '"')
		.replace(/&apos;/g, "'")
}
export default {
	recover,
	recoverNotNull,
	treeDataTranslate,
	escapeHTML,
	unescapeHTML
}
